/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.SalePoorProduct;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.SalePoorProductRepository;
import me.zhengjie.modules.system.service.SalePoorProductService;
import me.zhengjie.modules.system.service.dto.SalePoorProductDto;
import me.zhengjie.modules.system.service.dto.SalePoorProductQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.SalePoorProductMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author 李忠臻
* @date 2024-02-29
**/
@Service
@RequiredArgsConstructor
public class SalePoorProductServiceImpl implements SalePoorProductService {

    private final SalePoorProductRepository salePoorProductRepository;
    private final SalePoorProductMapper salePoorProductMapper;

    @Override
    public PageResult<SalePoorProductDto> queryAll(SalePoorProductQueryCriteria criteria, Pageable pageable){
        Page<SalePoorProduct> page = salePoorProductRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(salePoorProductMapper::toDto));
    }

    @Override
    public List<SalePoorProductDto> queryAll(SalePoorProductQueryCriteria criteria){
        return salePoorProductMapper.toDto(salePoorProductRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public SalePoorProductDto findById(Long id) {
        SalePoorProduct salePoorProduct = salePoorProductRepository.findById(id).orElseGet(SalePoorProduct::new);
//        ValidationUtil.isNull(salePoorProduct.getId(),"SalePoorProduct","id",id);
        return salePoorProductMapper.toDto(salePoorProduct);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(SalePoorProduct resources) {
        salePoorProductRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SalePoorProduct resources) {
//        SalePoorProduct salePoorProduct = salePoorProductRepository.findById(resources.getId()).orElseGet(SalePoorProduct::new);
//        ValidationUtil.isNull( salePoorProduct.getId(),"SalePoorProduct","id",resources.getId());
//        salePoorProduct.copy(resources);
//        salePoorProductRepository.save(salePoorProduct);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            salePoorProductRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<SalePoorProductDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SalePoorProductDto salePoorProduct : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("型号", salePoorProduct.getModel());
            map.put("距今天数", salePoorProduct.getFromNowDay());
            map.put("采购次数", salePoorProduct.getPurchaseTime());
            map.put("采购数量", salePoorProduct.getQuantity());
//            map.put("创建者", salePoorProduct.getCreateBy());
//            map.put("更新者", salePoorProduct.getUpdateBy());
//            map.put("创建日期", salePoorProduct.getCreateTime());
//            map.put("更新时间", salePoorProduct.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}